<?php

class Core_Basket
{
	public $locale;
	public $userData;
	public $sess;
	public $count;
	public $sum;
	public $sumDb;
	public $iden;
	public $items;
	public $delivery;
	public $sumInt = 0;
	public $deliveryInt = 0;
	public $curr;
	public $priceRank = 1;
	// czy jest w koszyku co najmniej 1 produkt normlany
	public $isAnySpecialProduct;
	// czy jest w koszyku co najmniej 1 produkt specjalny
	public $isAnyNormalProduct;
	public $currency = array(
		'cs' => 'CZK',
		'en' => 'EUR'
	);
	
	public function __construct( $locale, $userData, $priceRank = 1 ) {
		$this->locale = $locale;
		$this->userData = $userData;
		$this->priceRank = $priceRank;
		
		$this->sess = new Zend_Session_Namespace( 'sessionData' );

		$this->count = 0;
		$this->sum = 0;
		$this->iden = $this->sess->basket['iden'];
		$this->items = 0;
		$this->isAnySpecialProduct = false;
		$this->isAnyNormalProduct = false;
	}
	
	public function clear() {
		$this->count = 0;
		$this->sum = 0;
		$this->sess->unsetAll();
		$this->items = 0;
		
        $helpSess = new Zend_Session_Namespace( 'form' );
		$helpSess->unsetAll();
	}
	
	public function sumDb( $checkPack = false ) {

		$Products = new Products();
		
		if( $checkPack ) {
			$area = 0;
			$weight = 0;
		}

		$range = array(
			'r1' => 'yes'
		);

		$range2sess = false;

		if( $this->userData ) {
			$sessRanks = new Zend_Session_Namespace( 'ranks' );

			if( count( $sessRanks->data ) > 0 ) {
				$range2sess = $sessRanks->data;
				$range = false;
			} 
		}

		$count = 0;
		$sum = 0;

		foreach( $this->sess->basket['items'] as $value ) {
			foreach( $value as $elem ) {
				if( $elem['special'] ) {
				    $this->isAnySpecialProduct = true;
					$item = $Products->getItemSpecialService( $elem['info']['ps_id'], array(
						'r' . $this->priceRank => 'yes'
					) );
				} else {
				    $this->isAnyNormalProduct = true;
					$item = $Products->getItemService( $elem['info']['p_id'], $range, $range2sess );
				}
				if( $item =  $item->getFirst() ) {
					//echo $item['Dimension'] . '<br />';
					//echo $elem['dem'].'<br />';

					if( !$elem['special'] ) {
						$i = 0;
						foreach( $item['Dimension']->toArray() as $dem ) {
							if( $dem['d_id'] == $elem['dem'] ) {
								break;
							}
							$i++;
						}
	
						if( $elem['cnt'] > $dem['stock'] ) {
							$roz = $elem['cnt'] - $dem['stock'];
							$elem['cnt'] = $elem['cnt'] - $roz;
							$this->sess->basket['items'][$elem['info']['p_id']][$elem['dem']]['cnt'] = $elem['cnt'];
						}
						
						
						if( $checkPack && $item['no_size'] < 1 ) {
							$area = $area + ( ( $dem['x'] * $dem['z'] ) * $elem['cnt'] );
							$weight = $weight + $dem['weight'];
						}

						$am = $this->_calc( $item, $i, $this->locale, true );
						
					} else {
						$am = $this->_calcSpecial( $item, $this->priceRank, $this->locale, true );
					}
					
					$am[0] = $am[0] * $elem['cnt'];

					$sum = $sum + $am[0];
					$count = $count + $elem['cnt'];
				}
			}
		}

		if( ( $this->priceRank === 1 ) || ( $this->priceRank === 5 ) ) {
			$Settings = new Settings();
			
			$itemSettings = $Settings->getItemByNameService( 'freeprice' );
			if( $sum > $itemSettings->getFirst()->value ) {
				$checkPack = false;
			}
		}

		if( $checkPack ) {
			$Pack = new Core_Pack( $area, $weight );
			$Pack->calc();
			$this->delivery = $Pack->result;
			
			$Currency = new Currency();
			$items = $Currency->getListService()->toArray();
			$crroz = isSet( $this->currency[$this->locale] ) ? ' ' . $this->currency[$this->locale] : ' PLN';
			$currencyValue = false;
			if( $this->locale != 'pl' ) {
				foreach( $items as $value ) {
					if( $value['name'] == $this->currency[$this->locale] ) {
						$currencyValue = $value['value'];
					}
				}
			}
			$vat = ( $this->delivery * 22 ) / 100;
			$amo = $this->delivery + $vat;
			if( $currencyValue ) {
				$amo = $amo / $currencyValue;
			}

			$this->deliveryInt = $amo;
			$this->delivery = number_format( $amo, 2, '.', ' ') . $crroz;
		} else {
			$croz = isSet( $am[1] ) ? $am[1] : '';
			$this->deliveryInt = 0;
			$this->delivery = number_format( 0, 2, '.', ' ') . $croz;
		}

		$croz = isSet( $am[1] ) ? $am[1] : '';
		$this->sumInt = $sum;
		$this->sum = number_format( $sum, 2, '.', ' ') . ' ' . $croz;
		$this->count = $count;
		$this->curr = $croz;
		
		if($this->isAnyNormalProduct==false && $this->isAnySpecialProduct==true)
		{
		    // w koszyku sa tylko produkty specjalne (1 lub wiecej)
		    // wiec koszt przesylki = 0
			$this->deliveryInt = 0;
			$this->delivery = number_format( 0, 2, '.', ' ') . $croz;
		}
		
	}
	
	public function sumAll() {
		
		$count = 0;
		$sum = 0;

		foreach( $this->sess->basket['items'] as $value ) {
			foreach( $value as $elem ) {
				if( $elem['special'] ) {
				    $this->isAnySpecialProduct = true;
					$am = $this->_calcSpecial( $elem['info'], $this->priceRank, $this->locale, true );
					$this->sess->basket['items'][$elem['info']['ps_id']][$elem['dem']]['rank'] = $this->priceRank;
				} else {
				     $this->isAnyNormalProduct = true;
					$am = $this->_calc( $elem['info'], $elem['free'], $this->locale, true );
					$this->sess->basket['items'][$elem['info']['p_id']][$elem['dem']]['rank'] = $am[2];
				}
				$am[0] = $am[0] * $elem['cnt'];
				
				$sum = $sum + $am[0];
				$count = $count + $elem['cnt'];
			}
		}
		$croz = isSet( $am[1] ) ? $am[1] : '';
		$this->sum = number_format( $sum, 2, '.', ' ') . ' ' . $croz;
		$this->count = $count;
	}
	
	public function add( $id, $dem, $count, $special ) {
		
		if( $special ) {
			
			$Products = new Products();
	
			if( !$this->userData ) {
				$item = $Products->getItemSpecialService( $id, array(
					'r1' => 'yes'
				) );
			} else {
				$priceRank = ( $this->userData->price_rank ) ? $this->userData->price_rank : 1;
				$item = $Products->getItemSpecialService( $id, array(
					'r' . $priceRank => 'yes'
				) );
			}
	
			if( $item = $item->getFirst() ) {
				
				$i = 0;
	
				if( isSet( $this->sess->basket['items'][$id][$dem] ) ) {
					$this->sess->basket['items'][$id][$dem]['cnt'] = $this->sess->basket['items'][$id][$dem]['cnt'] + $count;
				} else {
					$this->sess->basket['items'][$id][$dem] = array(
						'info' => $item,
						'cnt' => $count,
						'dem' => $dem,
						'free' => $i,
						'special' => $special
					);
				}
			}
			
		} else {
		
			$Products = new Products();
	
			if( !$this->userData ) {
				$item = $Products->getItemService( $id, array(
					'r1' => 'yes'
				) );
			} else {
				$sessRanks = new Zend_Session_Namespace( 'ranks' );
				
				if( count( $sessRanks->data ) > 0 ) {
					$item = $Products->getItemService( $id, false, $sessRanks->data );
				} else {
					$item = $Products->getItemService( $id, array(
						'r1' => 'yes'
					) );
				}
			}
	
			if( $item = $item->getFirst() ) {
				
				$i = 0;
				foreach( $item['Dimension'] as $value ) {
					//Core_Debug::pr( $value );
					if( $value['d_id'] == $dem ) {
						break;
					}
					$i++;
				}
	
				if( isSet( $this->sess->basket['items'][$id][$dem] ) ) {
					if( $item['Dimension'][$i]['stock'] >= $this->sess->basket['items'][$id][$dem]['cnt'] ) {
						$this->sess->basket['items'][$id][$dem]['cnt'] = $this->sess->basket['items'][$id][$dem]['cnt'] + $count;
					} else {
						return;
					}
				} else {
					if( $item['Dimension'][$i]['stock'] >= $count ) {
						$this->sess->basket['items'][$id][$dem] = array(
							'info' => $item,
							'cnt' => $count,
							'dem' => $dem,
							'free' => $i,
							'special' => 0
						);
					} else {
						return;
					}
				}
				
			}
		}

	}
	
	public function plus( $id, $free, $plus ) {
		if( $plus > 0 ) {
			foreach( $this->sess->basket['items'] as $value ) {
				foreach( $value as $key => $elem ) {
					
					$iden = ( $elem['special'] ) ? 'ps_id' : 'p_id';
					
					if( ( $elem['info'][$iden] == $id ) && ( $elem['free'] == $free ) ) {
						$this->sess->basket['items'][$id][$key]['cnt'] = $this->sess->basket['items'][$id][$key]['cnt'] + $plus;
						break;
					}
				}
			}
		}
	}
	
	public function amount( $id, $free, $amount) 
	{
		if( $amount > 0 ) 
		{
			foreach( $this->sess->basket['items'] as $value ) {
				foreach( $value as $key => $elem ) {
					
					$iden = ( $elem['special'] ) ? 'ps_id' : 'p_id';
					
					if( ( $elem['info'][$iden] == $id ) && ( $elem['free'] == $free ) ) 
					{
						$this->sess->basket['items'][$id][$key]['cnt'] = $amount;
						break;
					}
				}
			}
		}
	}
	
	public function minus( $id, $free, $minus ) {
		
		if( $minus > 0 ) {
			foreach( $this->sess->basket['items'] as $value ) {
				foreach( $value as $key => $elem ) {
					
					$iden = ( $elem['special'] ) ? 'ps_id' : 'p_id';
					
					if( ( $elem['info'][$iden] == $id ) && ( $elem['free'] == $free ) ) {
						$this->sess->basket['items'][$id][$key]['cnt'] = $this->sess->basket['items'][$id][$key]['cnt'] - $minus;
						break;
					}
				}
			}
		}
	}
	
	public function delete( $id, $dem ) {
		foreach( $this->sess->basket['items'] as $value ) {
			foreach( $value as $key => $elem ) {
				
				$iden = ( $elem['special'] ) ? 'ps_id' : 'p_id';
				
				if( ( $elem['info'][$iden] == $id ) && ( $elem['dem'] == $dem ) ) {
					unset( $this->sess->basket['items'][$id][$key] );
					break;
				}
			}
		}
	}
	
	private function _calc( $item, $free, $locale, $cur = false ) {		

		$currencyValue = false;
		if( $cur ) {
			$Currency = new Currency();
			$items = $Currency->getListService()->toArray();
			$crroz = isSet( $this->currency[$locale] ) ? ' ' . $this->currency[$locale] : ' PLN';
			
			if( $locale != 'pl' ) {
				foreach( $items as $value ) {
					if( $value['name'] == $this->currency[$locale] ) {
						$currencyValue = $value['value'];
					}
				}
			}

		} else {
			$crroz = '';
		}
		$sess = new Zend_Session_Namespace( 'ranks' );
		
		
		$discount = (isSet($this->sess->basket['discount'])) ? $this->sess->basket['discount']: 0;
		if( count( $sess->data ) > 0 ) {
			foreach( $sess->data as $value ) {
				if( $value['manufacturers_m_id'] == $item['manufacturers_m_id'] ) {
					if( !$currencyValue ) {
						$vat = 0;
						if( $item['Vat']['rate'] > 0 ) {
							$vat = ( ($item['Dimension'][$free]['price' . $value['rank_id']]  - $item['Dimension'][$free]['price' . $value['rank_id']]*$discount/100)* $item['Vat']['rate'] ) / 100;
						}

						$am = $vat + $item['Dimension'][$free]['price' . $value['rank_id']]  - $item['Dimension'][$free]['price' . $value['rank_id']]*$discount/100;

						return array( $am, $crroz, $value['rank_id'] );
					} else {
						$vat = 0;
						if( $item['Vat']['rate'] > 0 ) {
							$vat = ( ($item['Dimension'][$free]['price' . $value['rank_id']]   - $item['Dimension'][$free]['price' . $value['rank_id']]*$discount/100)* $item['Vat']['rate'] ) / 100;
						}

						$am = $vat + $item['Dimension'][$free]['price' . $value['rank_id']]   - $item['Dimension'][$free]['price' . $value['rank_id']]*$discount/100;

						$am = $am / $currencyValue;
						return array( $am, $crroz, $value['rank_id'] );
					}
				}
			}
		} else {
			if( !$currencyValue ) {
				$vat = 0;
				if( $item['Vat']['rate'] > 0 ) {
					$vat = ( ($item['Dimension'][$free]['price1']   - $item['Dimension'][$free]['price1']*$discount/100) * $item['Vat']['rate'] ) / 100;
				}

				$am = $vat + $item['Dimension'][$free]['price1']  - $item['Dimension'][$free]['price1']*$discount/100;

				return array( $am, $crroz, 1 );
			} else {
				$vat = 0;
				if( $item['Vat']['rate'] > 0 ) {
					$vat = ( ($item['Dimension'][$free]['price1']   - $item['Dimension'][$free]['price1']*$discount/100)* $item['Vat']['rate'] ) / 100;
				}

				$am = $vat + $item['Dimension'][$free]['price1']  - $item['Dimension'][$free]['price1']*$discount/100;

				$am = $am / $currencyValue;
				return array( $am, $crroz, 1 );
			}
		}
	}
	
	private function _calcSpecial( $item, $free, $locale, $cur = false ) {		

		$currencyValue = false;
		if( $cur ) {
			$Currency = new Currency();
			$items = $Currency->getListService()->toArray();
			$crroz = isSet( $this->currency[$locale] ) ? ' ' . $this->currency[$locale] : ' PLN';
			
			if( $locale != 'pl' ) {
				foreach( $items as $value ) {
					if( $value['name'] == $this->currency[$locale] ) {
						$currencyValue = $value['value'];
					}
				}
			}

		} else {
			$crroz = '';
		}
		$discount = (isSet($this->sess->basket['discount'])) ? $this->sess->basket['discount']: 0;
		if( !$currencyValue ) {
			$vat = 0;
			if( $item['Vat']['rate'] > 0 ) {
				$vat = ( ($item['price' . $free]   - $item['price' . $free]*$discount/100)* $item['Vat']['rate'] ) / 100;
			}

			$am = $vat + $item['price' . $free]  - $item['price' . $free]*$discount/100;

			return array( $am, $crroz );
		} else {
			$vat = 0;
			if( $item['Vat']['rate'] > 0 ) {
				$vat = ( ($item['price' . $free]   - $item['price' . $free]*$discount/100)* $item['Vat']['rate'] ) / 100;
			}

			$am = $vat + $item['price' . $free]  - $item['price' . $free]*$discount/100;

			$am = $am / $currencyValue;
			return array( $am, $crroz );
		}
	}
}